<?php
namespace app\common\model;
use think\Model;
class Base extends Model
{
    protected function initialize()
    {
        parent::initialize();
    }

    /* 单条 */
    public static function getOne ($where,$field="*",$ord=false) {
        if($ord) {
            $res = self::where($where)->field($field)->order($ord)->find();
        }else{
            $res = self::where($where)->field($field)->find();
        }
        if(!$res) {
            return false;
        }
        return $res->toArray();
    }
    /* 单条 */
    public static function getValue ($where,$field="*",$ord=false) {
        if($ord) {
            $res = self::where($where)->field($field)->order($ord)->value($field);
        }else{
            $res = self::where($where)->field($field)->value($field);
        }
        if(!$res) {
            return false;
        }
        return $res;
    }
    /* 单条 */
    public static function getColumn ($where,$field="*",$ord=false) {
        if($ord) {
            $res = self::where($where)->order($ord)->column($field);
        }else{
            $res = self::where($where)->column($field);
        }
        if(!$res) {
            return false;
        }
        return $res;
    }
    /* 单条 */
    public static function getSum ($where,$field="*",$ord=false) {
        if($ord) {
            $res = self::where($where)->field($field)->order($ord)->sum($field);
        }else{
            $res = self::where($where)->field($field)->sum($field);
        }
        if(!$res) {
            return false;
        }
        return $res;
    }

    /* 单条 */
    public static function getOneById ($id,$field="*") {
        $res = self::where('id',$id)->field($field)->find();
        if(!$res) {
            return false;
        }
        return $res->toArray();
    }

    /* 多条 */
    public static function getAll ($where,$orderby='id desc',$field="*") {
        $res = self::where($where)->field($field)->order($orderby)->select();
        if($res->isEmpty()) {
            return false;
        }
        return $res->toArray();
    }

    /* 分页 */
    public static function getPage ($where,$page=1,$size=20,$orderby='id desc',$field="*") {
        $res = self::where($where)->page($page,$size)->order($orderby)->field($field)->select();
        if($res->isEmpty()) {
            return false;
        }
        return $res->toArray();
    }

    public static function getCount ($where) {
        return $res = self::where($where)->count();
    }

    public static function setFinc ($where,$field,$number=1) {
        return $res = self::where($where)->setInc($field,$number);
    }
    public static function setGetLasetId ($data) {
        return $res = self::where([])->insertGetId($data);
    }
    
}